var AccTable = function(pid)
{
	this.rootid = pid;
	this.rootnode = document.getElementById(this.rootid);
	this.instancename = this.rootnode.getAttribute("instancename");
	this.filename = "";

	this.dbfilenameboxid = this.instancename + (new Date()).getTime().toString() + "filename";
	this.sqlboxid = this.instancename + (new Date()).getTime().toString() + "sql";
	this.readbuttonid = this.instancename + (new Date()).getTime().toString() + "read";
	this.writebuttonid = this.instancename + (new Date()).getTime().toString() + "write";
	this.schemabuttonid = this.instancename + (new Date()).getTime().toString() + "schema";
	this.resultareaid = this.instancename + (new Date()).getTime().toString() + "result";
	this.schemaboxid = this.instancename + (new Date()).getTime().toString() + "schemabox";

	this.sqlrid = this.instancename + (new Date()).getTime().toString() + "sqlr";
	//this.oprid = this.instancename + (new Date()).getTime().toString() + "opr";


	this.display = function()
	{
		this.rootnode.innerHTML = "";
		var t = this.rootnode.appendChild(document.createElement("table"));
		var r = t.appendChild(document.createElement("tr"));
		var d = r.appendChild(document.createElement("td"));
		var myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "file");
		myi.setAttribute("id", this.dbfilenameboxid);
		myi.setAttribute("size", "100");
		myi.setAttribute("onchange", this.instancename + ".reflectfilestatus();");
		myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.color = "blue";
		myi.style.border = "none";
		myi.setAttribute("value", "lock");
		myi.setAttribute("onclick", "var mya = document.getElementById('" + this.dbfilenameboxid + "'); mya.disabled = !mya.disabled;");

		var sel = d.appendChild(document.createElement("select"));
		sel.setAttribute("id", this.schemaboxid);
		sel.setAttribute("onfocus", "this.style.background = 'lightyellow';");
		sel.setAttribute("onblur", "this.style.background = 'white';");
		sel.style.display = "none";
		sel.options.length = 0;
		var myse = new SchemaEnum();
		for(var icnt = 0; icnt < myse.items.length; icnt++)
		{
			var tempe = myse.items[icnt];
			sel.options[sel.options.length] = new Option(tempe.alias, tempe.alias);
		}

		myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.color = "blue";
		myi.style.border = "none";
		myi.style.display = "none";
		myi.setAttribute("id", this.schemabuttonid);
		myi.setAttribute("value", "GetSchema");
		myi.setAttribute("onclick", this.instancename + ".getschema();");

		r = t.appendChild(document.createElement("tr"));
		r.setAttribute("id", this.sqlrid);
		r.style.display = "none";
		d = r.appendChild(document.createElement("td"));
		myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "text");
		myi.setAttribute("placeholder", "input sql");
		myi.setAttribute("id", this.sqlboxid);
		myi.setAttribute("size", "100");
		myi.setAttribute("onfocus", "this.style.background = 'lightyellow';");
		myi.setAttribute("onblur", "this.style.background = 'white';");

		//r = t.appendChild(document.createElement("tr"));
		//r.setAttribute("id", this.oprid);
		//r.style.display = "none";
		//d = r.appendChild(document.createElement("td"));
		myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.color = "blue";
		myi.style.border = "none";
		myi.style.display = "none";
		myi.setAttribute("id", this.readbuttonid);
		myi.setAttribute("value", "Read");
		myi.setAttribute("onclick", this.instancename + ".read();");

		myi = d.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.color = "blue";
		myi.style.border = "none";
		myi.style.display = "none";
		myi.setAttribute("id", this.writebuttonid);
		myi.setAttribute("value", "Write");
		myi.setAttribute("onclick", this.instancename + ".write();");


		r = t.appendChild(document.createElement("tr"));
		d = r.appendChild(document.createElement("td"));
		var innt = d.appendChild(document.createElement("table"));
		var innr = innt.appendChild(document.createElement("tr"));
		var innd = innr.appendChild(document.createElement("tr"));
		innd.setAttribute("id", this.resultareaid);
	}
	this.reflectfilestatus = function()
	{
		this.filename = document.getElementById(this.dbfilenameboxid).value;
		var fu = new FileUtil();
		if(fu.fso.GetExtensionName(this.filename).toUpperCase() != "ACCDB")
		{
			document.getElementById(this.dbfilenameboxid).value = "";
			this.filename = "";
		}

		var nextstate = "none";
		if(this.filename)
		{
			if(this.filename != "")
			{
				nextstate = "";
			}
		}
		var names = new Array();
		names.length = 0;
		names[names.length] = this.readbuttonid;
		names[names.length] = this.writebuttonid;
		names[names.length] = this.schemabuttonid;
		names[names.length] = this.sqlrid;
		names[names.length] = this.schemaboxid;
		//names[names.length] = this.oprid;
		for(var icnt = 0; icnt < names.length; icnt++)
		{
			var namee = names[icnt];
			document.getElementById(namee).style.display = nextstate;
		}
	}
	this.getschema = function()
	{
		var criteria = document.getElementById(this.schemaboxid).value;
		var acc = new AccessClient(this.filename);
		var rl = acc.getschema(criteria);
		this.displayresult(rl);
	}
	this.read = function()
	{
		var sql = document.getElementById(this.sqlboxid).value;
		var acc = new AccessClient(this.filename);
		var rl = acc.executeselectquery(sql);
		this.displayresult(rl);
	}
	this.write = function()
	{
		var sql = document.getElementById(this.sqlboxid).value;
		var acc = new AccessClient(this.filename);
		acc.executenonquery(sql);
	}
	this.displayresult = function(rl)
	{
		var mya = document.getElementById(this.resultareaid);
		mya.innerHTML = "";
		var t = mya.appendChild(document.createElement("table"));
		var r = null;
		var d = null;
		if(rl.length > 0)
		{
			var su = new StrUtil();
			var cols = new Array();
			cols.length = 0;
			var re = rl[0];
			r = t.appendChild(document.createElement("tr"));
			r.style.background = "lightsteelblue";
			for(var pn in re)
			{
				d = r.appendChild(document.createElement("td"));
				d.innerText = pn;
				cols[cols.length] = pn;
			}
			for(var icnt = 0; icnt < rl.length; icnt++)
			{
				re = rl[icnt];
				r = t.appendChild(document.createElement("tr"));
				if(icnt % 2 == 0)
				{
					r.style.background = "lavender";
				}
				for(var ccnt = 0; ccnt < cols.length; ccnt++)
				{
					var cole = cols[ccnt];
					d = r.appendChild(document.createElement("td"));
					try
					{
						d.innerHTML = su.replaceall(re[cole], "\n", "<br />");
					}
					catch(err)
					{
						d.innerText = re[cole];
					}
				}
			}
		}
	}
}
